﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using StackExchange.Redis;
using System.Collections.Generic;
using UtilLib;
using UtilPublic.Serialization;
using System;

namespace UtilPublic.Redis
{
    /// <summary>
    /// Set存储
    /// </summary>
    public partial class Rds
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="jobj"></param>
        public void SetAdd<T1, T2>(T1 key, T2 value, bool jobj = false) => RedisDb.SetAdd(Zk(key), ToText(value, jobj));
        /// <summary>
        /// 添加多个
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="values"></param>
        /// <param name="jobj"></param>
        public void SetAddRange<T1, T2>(T1 key, IEnumerable<T2> values, bool jobj = false)
        {
            if (jobj)
            {
                foreach (var value in values)
                {
                    RedisDb.SetAdd(Zk(key), value.Jstr());
                }
            }
            else
            {
                foreach (var value in values)
                {
                    RedisDb.SetAdd(Zk(key), value.ToString());
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="jobj"></param>
        public void SetRemove<T1, T2>(T1 key, T2 value, bool jobj = false) => RedisDb.SetRemove(Zk(key), ToText(value, jobj));
        /// <summary>
        /// 删除并返回随机一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public RedisValue SetPop<T>(T key) => RedisDb.SetPop(Zk(key));
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="jobj"></param>
        /// <returns></returns>
        public bool SetExist<T1, T2>(T1 key, T2 value, bool jobj = false) => RedisDb.SetContains(Zk(key), ToText(value, jobj));
        /// <summary>
        /// 长度
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public long SetLength<T>(T key) => RedisDb.SetLength(Zk(key));
        /// <summary>
        /// 清空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool SetClear<T>(T key) => RedisDb.KeyDelete(Zk(key));
        /// <summary>
        /// 元素列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public RedisValue[] SetMembers<T>(T key) => RedisDb.SetMembers(Zk(key));
        /// <summary>
        /// 元素列表
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="result"></param>
        public void SetMembers<T1, T2>(T1 key, out List<T2> result)
        {
            result = new();
            try
            {
                var t = RedisDb.SetMembers(Zk(key));
                foreach (string n in t)
                {
                    result.Add(n.Jobj<T2>());
                }
            }
            catch
            {
                Zloger.Fatal($"SetMembers Error {key}");
            }
        }
        /// <summary>
        /// 随机元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public RedisValue SetRandomMember<T>(T key) => RedisDb.SetRandomMember(Zk(key));
        /// <summary>
        /// 随机多个元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="count"></param>
        public RedisValue[] SetRandomMembers<T>(T key, long count) => RedisDb.SetRandomMembers(Zk(key), count);

        /// <summary>
        /// 设置为多个
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="values"></param>
        /// <param name="jobj"></param>
        public void SetSetRange<T1, T2>(T1 key, IEnumerable<T2> values, bool jobj = false)
        {
            //这两个方法会重新判断key
            SetClear(key);
            SetAddRange(key, values, jobj);
        }


    }
}